<?xml version="1.0" encoding="UTF-8" ?>
<%@page import="fr.utbm.gi.in56.ads.AdType"%>
<%@page import="fr.utbm.gi.in56.ads.AdState"%>
<%@ page
	contentType="text/html;charset=UTF-8"
	pageEncoding="UTF-8"
	import="java.util.List"
	import="java.util.HashMap"
	import="fr.utbm.gi.in56.vehicle.BrandEntry"
	import="fr.utbm.gi.in56.vehicle.ModelEntry"
	import="fr.utbm.gi.in56.error.ErrorType"
	import="fr.utbm.gi.in56.ads.AdSell"
	import="fr.utbm.gi.in56.ads.AdBuy"
%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <link rel="stylesheet" media="screen" type="text/css" title="Style" href="style.css" />
        <script type="text/javascript" src="ressources/scripts/modelList.js"></script>
        <script type="text/javascript" src="ressources/scripts/dynamicInput.js"></script> 
        <title>L'Entre-Pot</title>
    </head>
    <body>
        <div id ="container">
            <jsp:directive.include file="header.jsp"/>
            <jsp:directive.include file="topMenu.jsp"/>
            <div id="core">
            	<%!
            		public String displayBuyList( List< AdBuy > _buyList, boolean _mine, List< List< String > > _userAllowed )
            		{
	            		String result = new String();
	            		
	            		for( int index = 0; index < _buyList.size(); ++index )
						{
	            			AdBuy currentAd = _buyList.get( index );
							AdState adState = currentAd.getAdState();
							
							result += "<div id=\"resultEntry\">";
							
							result += "<div id=\"resultEntryTitle\">"+
								currentAd.getTitle()+
							"</div>";
							
							result += "<div id=\"resultEntryCore\">"+
							"<div id=\"resultEntryLeft\">";
							
							if ( !currentAd.getBrand().isEmpty() )
							{
								result += "<div id=\"resultEntryInfo\">"+
								"<div id=\"resultEntryInfoTitle\">"+
								"Marque"+
								"</div>"+
								"<div id=\"resultEntryInfoValue\">"+
								currentAd.getBrand()+
								"</div>"+
							"</div>";
							}
							
							if ( !currentAd.getModel().isEmpty() )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\""+
									">Modèle"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getModel()+
									"</div>"+
								"</div>";
							}
							
							if ( currentAd.getVehicleYearMin() != 0 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Année min"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getVehicleYearMin()+
									"</div>"+
								"</div>";
							}
							
							if ( currentAd.getVehicleYearMax() != 0 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Année max"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getVehicleYearMax()+
									"</div>"+
								"</div>";
							}
							
							if ( currentAd.getKmNbMin() != -1 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Kilométrage min"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getKmNbMin() + "km"+
									"</div>"+
								"</div>";
							}
							
							if ( currentAd.getKmNbMax() != -1 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Kilométrage max"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getKmNbMax() + "km"+
									"</div>"+
								"</div>";
							}
							
							if ( !currentAd.getEnergy().isEmpty() )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Énergie"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getEnergy()+
									"</div>"+
								"</div>";
							}
							
							int isAutomatic = currentAd.isAutomatic();
							if ( isAutomatic != 2 && isAutomatic != -1 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Boîte de vitesse"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">";
								if ( isAutomatic == 1 )
								{
									result += "Automatique";
								}
								else if ( isAutomatic == 0 )
								{
									result += "Manuelle";
								}
								
								result += "</div>"+
								"</div>";
							}
							
							if ( currentAd.getPriceMin() != -1 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Prix min"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getPriceMin() + " €" +
									"</div>"+
								"</div>";
							}
							
							if ( currentAd.getPriceMax() != -1 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Prix max"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getPriceMax() + " €"+
									"</div>"+
								"</div>";
							}
							
							result += "</div>"+
							"<div id=\"resultEntryCenter\">"+
								currentAd.getDescription()+
							"</div>"+
						"</div>"+
						"<div id=\"resultEntryFooter\">";
						
							if ( adState == AdState.OPENED && _mine )
							{
								result += "Confirmer le vendeur : "+
								"<form style=\"display:inline;\" method=\"post\" action=\"AdStateServlet\" name=closeAd>"+
									"<input type=\"hidden\" name=\"adId\" value=" + currentAd.getId() + ">"+
									"<input type=\"hidden\" name=\"adType\" value=\"buy\">"+
									"<input type=\"hidden\" name=\"action\" value=\"close\">"+
									"<select name=\"user\" id=\"user\">";
									for ( String currentUser : _userAllowed.get( index ) )
									{
										result += "<option value=\""+ currentUser + "\">" + currentUser + "</option>";
									}
								result += "</select>"+
									"<input type=\"submit\" value=\"Valider\" name=\"rate\"/>"+
								"</form>";
							}
							else if ( adState == AdState.WAITING_FOR_END_CONFIRMATION )
							{
								if ( _mine )
								{
									result += "En attente de confirmation de la part de " + currentAd.getLoginSeller();
								}
								else
								{
									result += "Confirmer être le vendeur : "+
									"<form style=\"display:inline;\"method=\"post\" action=\"AdStateServlet\" name=closeAd>"+
										"<input type=\"hidden\" name=\"adId\" value=" + currentAd.getId() + ">"+
										"<input type=\"hidden\" name=\"adType\" value=\"buy\">"+
										"<input type=\"hidden\" name=\"action\" value=\"confirm\">"+
										"<input type=\"hidden\" name=\"user\" value=\"" + currentAd.getLoginBuyer() + "\">"+
										"<input type=\"submit\" value=\"Valider\" name=\"confirm\"/>"+
									"</form>";
								}
							}
							else if ( adState == AdState.WAITING_FOR_RATING )
							{
								if ( _mine )
								{
									result += "<form method=\"post\" action=\"AdStateServlet\" name=\"rateUser\">"+
									"<input type=\"hidden\" name=\"adId\" value=\"" + currentAd.getId() + "\">"+
									"<input type=\"hidden\" name=\"adType\" value=\"buy\">"+
									"<input type=\"hidden\" name=\"action\" value=\"rate\">"+
									"<input type=\"hidden\" name=\"user\" value=\"" + currentAd.getLoginSeller() + "\">"+
									"<input type=\"radio\" name=\"rating\" value=\"0\" checked/>0";
									for ( int i = 1; i <= 5; ++i )
									{
										result += "<input type=\"radio\" name=\"rating\" value=\"" + i + "\"/>" + i;
									}
									
									if ( _mine )
									result += "<input type=\"submit\" value=\"Noter le vendeur\" name=\"rate\"/>"+
									"</form>";
								}
								else
								{
									result += "<form method=\"post\" action=\"AdStateServlet\" name=\"rateUser\">"+
									"<input type=\"hidden\" name=\"adId\" value=\"" + currentAd.getId() + "\">"+
									"<input type=\"hidden\" name=\"adType\" value=\"buy\">"+
									"<input type=\"hidden\" name=\"action\" value=\"rate\">"+
									"<input type=\"hidden\" name=\"user\" value=\"" + currentAd.getLoginBuyer() + "\">"+
									"<input type=\"radio\" name=\"rating\" value=\"0\" checked/>0";
									for ( int i = 1; i <= 5; ++i )
									{
										result += "<input type=\"radio\" name=\"rating\" value=\"" + i + "\"/>" + i;
									}
									
									if ( _mine )
									result += "<input type=\"submit\" value=\"Noter l'acheteur\" name=\"rate\"/>"+
									"</form>";
								}
							}
							else if ( adState == AdState.WAITING_FOR_BUYER_RATING )
							{
								if ( _mine )
								{
									result += "<form method=\"post\" action=\"AdStateServlet\" name=\"rateUser\">"+
									"<input type=\"hidden\" name=\"adId\" value=\"" + currentAd.getId() + "\">"+
									"<input type=\"hidden\" name=\"adType\" value=\"buy\">"+
									"<input type=\"hidden\" name=\"action\" value=\"rate\">"+
									"<input type=\"hidden\" name=\"user\" value=\"" + currentAd.getLoginSeller() + "\">"+
									"<input type=\"radio\" name=\"rating\" value=\"0\" checked/>0";
									for ( int i = 1; i <= 5; ++i )
									{
									result += "<input type=\"radio\" name=\"rating\" value=\"" + i + "\"/>" + i;
									}
									result += "<input type=\"submit\" value=\"Noter le vendeur\" name=\"rate\"/>"+
									"</form>";
								}
								else
								{
									result += "En attente de note de la part de " + currentAd.getLoginBuyer();
								}
							}
							else if ( adState == AdState.WAITING_FOR_SELLER_RATING )
							{
								if ( _mine )
								{
									result += "En attente de note de la part de " + currentAd.getLoginSeller();
								}
								else
								{
									result += "<form method=\"post\" action=\"AdStateServlet\" name=\"rateUser\">"+
									"<input type=\"hidden\" name=\"adId\" value=\"" + currentAd.getId() + "\">"+
									"<input type=\"hidden\" name=\"adType\" value=\"buy\">"+
									"<input type=\"hidden\" name=\"action\" value=\"rate\">"+
									"<input type=\"radio\" name=\"rating\" value=\"0\" checked/>0";
									for ( int i = 1; i <= 5; ++i )
									{
									result += "<input type=\"radio\" name=\"rating\" value=\"" + i + "\"/>" + i;
									}
									result += "<input type=\"submit\" value=\"Noter l'acheteur\" name=\"rate\"/>"+
								"</form>";
								}
							}
							else if ( adState == AdState.CLOSED )
							{
								if ( _mine )
								{
									result += "Annonce conclue avec " + currentAd.getLoginSeller();
								}
								else
								{
									result += "Annonce conclue avec " + currentAd.getLoginBuyer();
								}
							}
							result += "</div>"+
							"</div>";
						}
	            		
	            		return result;
            		}
            	%>
            	<%!
            		public String displaySellList( List< AdSell > _sellList, boolean _mine, List< List< String > > _userAllowed )
            		{
	            		String result = new String();
	            		
	            		for( int index = 0; index < _sellList.size(); ++index )
						{
	            			AdSell currentAd = _sellList.get( index );
							AdState adState = currentAd.getAdState();
							
							result += "<div id=\"resultEntry\">";
							
							result += "<div id=\"resultEntryTitle\">"+
								currentAd.getTitle()+
								"</div>";
							
							result += "<div id=\"resultEntryCore\">"+
							"<div id=\"resultEntryLeft\">";
							
							if ( !currentAd.getBrand().isEmpty() )
							{
								result += "<div id=\"resultEntryInfo\">"+
								"<div id=\"resultEntryInfoTitle\">"+
								"Marque"+
								"</div>"+
								"<div id=\"resultEntryInfoValue\">"+
								currentAd.getBrand()+
								"</div>"+
							"</div>";
							}
							
							if ( !currentAd.getModel().isEmpty() )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\""+
									">Modèle"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getModel()+
									"</div>"+
								"</div>";
							}
							
							if ( currentAd.getVehicle_date() != 0 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Année"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getVehicle_date()+
									"</div>"+
								"</div>";
							}
							
							
							if ( currentAd.getKmNb() != -1 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Kilométrage"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getKmNb() + " km"+
									"</div>"+
								"</div>";
							}
							
							if ( !currentAd.getEnergy().isEmpty() )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Énergie"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getEnergy()+
									"</div>"+
								"</div>";
							}
							
							int isAutomatic = currentAd.isAutomatic();
							if ( isAutomatic != 2 && isAutomatic != -1 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Boîte de vitesse"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">";
								if ( isAutomatic == 1 )
								{
									result += "Automatique";
								}
								else if ( isAutomatic == 0 )
								{
									result += "Manuelle";
								}
								
								result += "</div>"+
								"</div>";
							}
							
							if ( currentAd.getPrice() != -1 )
							{
								result += "<div id=\"resultEntryInfo\">"+
									"<div id=\"resultEntryInfoTitle\">"+
									"Prix"+
									"</div>"+
									"<div id=\"resultEntryInfoValue\">"+
									currentAd.getPrice() + " €" +
									"</div>"+
								"</div>";
							}
							
							result += "</div>"+
							"<div id=\"resultEntryCenter\">"+
								currentAd.getDescription()+
							"</div>"+
							"<div id=\"resultEntryRight\">";
								if ( currentAd.isHasImage() )
								{
									result += "<img border=\"0\" src=\"ressources/images/" + currentAd.getId() + ".jpg\" width=\"150\" height=\"150\"/>";
								}
								else
								{
									result += "<img border=\"0\" src=\"ressources/images/no_image.jpg\" width=\"150\" height=\"150\"/>";
								}
								result += "</div>"+
						"</div>"+
						"<div id=\"resultEntryFooter\">";
						
							if ( adState == AdState.OPENED && _mine )
							{
								result += "Confirmer l'acheteur : "+
							"<form style=\"display:inline;\" method=\"post\" action=\"AdStateServlet\" name=closeAd>"+
								"<input type=\"hidden\" name=\"adId\" value=" + currentAd.getId() + ">"+
								"<input type=\"hidden\" name=\"adType\" value=\"sell\">"+
								"<input type=\"hidden\" name=\"action\" value=\"close\">"+
								"<select name=\"user\" id=\"user\">";
								for ( String currentUser : _userAllowed.get( index ) )
								{
									result += "<option value=\""+ currentUser + "\">" + currentUser + "</option>";
								}
								result += "</select>"+
								"<input type=\"submit\" value=\"Valider\" name=\"rate\"/>";
							}
							else if ( adState == AdState.WAITING_FOR_END_CONFIRMATION )
							{
								if ( _mine )
								{
									result += "En attente de confirmation de la part de " + currentAd.getLoginBuyer();
								}
								else
								{
									result += "Confirmer être l'acheteur : "+
									"<form style=\"display:inline;\"method=\"post\" action=\"AdStateServlet\" name=closeAd>"+
										"<input type=\"hidden\" name=\"adId\" value=" + currentAd.getId() + ">"+
										"<input type=\"hidden\" name=\"adType\" value=\"sell\">"+
										"<input type=\"hidden\" name=\"action\" value=\"confirm\">"+
										"<input type=\"hidden\" name=\"user\" value=\"" + currentAd.getLoginSeller() + "\">"+
										"<input type=\"submit\" value=\"Valider\" name=\"confirm\"/>"+
									"</form>";
								}
							}
							else if ( adState == AdState.WAITING_FOR_RATING )
							{
								if ( _mine )
								{
									result += "<form method=\"post\" action=\"AdStateServlet\" name=\"rateUser\">"+
									"<input type=\"hidden\" name=\"adId\" value=\"" + currentAd.getId() + "\">"+
									"<input type=\"hidden\" name=\"adType\" value=\"buy\">"+
									"<input type=\"hidden\" name=\"action\" value=\"rate\">"+
									"<input type=\"hidden\" name=\"user\" value=\"" + currentAd.getLoginBuyer() + "\">"+
									"<input type=\"radio\" name=\"rating\" value=\"0\" checked/>0";
									for ( int i = 1; i <= 5; ++i )
									{
										result += "<input type=\"radio\" name=\"rating\" value=\"" + i + "\"/>" + i;
									}
									
									if ( _mine )
									result += "<input type=\"submit\" value=\"Noter l'acheteur\" name=\"rate\"/>"+
									"</form>";
								}
								else
								{
									result += "<form method=\"post\" action=\"AdStateServlet\" name=\"rateUser\">"+
									"<input type=\"hidden\" name=\"adId\" value=\"" + currentAd.getId() + "\">"+
									"<input type=\"hidden\" name=\"adType\" value=\"buy\">"+
									"<input type=\"hidden\" name=\"action\" value=\"rate\">"+
									"<input type=\"hidden\" name=\"user\" value=\"" + currentAd.getLoginSeller() + "\">"+
									"<input type=\"radio\" name=\"rating\" value=\"0\" checked/>0";
									for ( int i = 1; i <= 5; ++i )
									{
										result += "<input type=\"radio\" name=\"rating\" value=\"" + i + "\"/>" + i;
									}
									
									if ( _mine )
									result += "<input type=\"submit\" value=\"Noter le vendeur\" name=\"rate\"/>"+
									"</form>";
								}
							}
							else if ( adState == AdState.WAITING_FOR_BUYER_RATING )
							{
								if ( _mine )
								{
									result += "En attente de note de la part de " + currentAd.getLoginBuyer();
								}
								else
								{
									result += "<form method=\"post\" action=\"AdStateServlet\" name=\"rateUser\">"+
									"<input type=\"hidden\" name=\"adId\" value=\"" + currentAd.getId() + "\">"+
									"<input type=\"hidden\" name=\"adType\" value=\"sell\">"+
									"<input type=\"hidden\" name=\"action\" value=\"rate\">"+
									"<input type=\"hidden\" name=\"user\" value=\"" + currentAd.getLoginSeller() + "\">"+
									"<input type=\"radio\" name=\"rating\" value=\"0\" checked/>0";
									for ( int i = 1; i <= 5; ++i )
									{
									result += "<input type=\"radio\" name=\"rating\" value=\"" + i + "\"/>" + i;
									}
									result += "<input type=\"submit\" value=\"Noter le vendeur\" name=\"rate\"/>"+
								"</form>";
								}
							}
							else if ( adState == AdState.WAITING_FOR_SELLER_RATING )
							{
								if ( _mine )
								{
									result += "<form method=\"post\" action=\"AdStateServlet\" name=\"rateUser\">"+
									"<input type=\"hidden\" name=\"adId\" value=\"" + currentAd.getId() + "\">"+
									"<input type=\"hidden\" name=\"adType\" value=\"sell\">"+
									"<input type=\"hidden\" name=\"action\" value=\"rate\">"+
									"<input type=\"radio\" name=\"rating\" value=\"0\" checked/>0";
									for ( int i = 1; i <= 5; ++i )
									{
									result += "<input type=\"radio\" name=\"rating\" value=\"" + i + "\"/>" + i;
									}
									result += "<input type=\"submit\" value=\"Noter l'acheteur\" name=\"rate\"/>"+
								"</form>";
								}
								else
								{
									result += "En attente de note de la part de " + currentAd.getLoginSeller();
								}
							}
							else if ( adState == AdState.CLOSED )
							{
								if ( _mine )
								{
									result += "Annonce conclue avec " + currentAd.getLoginBuyer();
								}
								else
								{
									result += "Annonce conclue avec " + currentAd.getLoginSeller();
								}
							}
							result += "</div>"+
							"</div>";
						}
	            		
	            		return result;
            		}
            	%>
            	
				<%
					List< AdBuy > adBuyList = (List< AdBuy >)request.getAttribute( "adBuyList" );
					List< AdBuy > adOtherBuyList = (List< AdBuy >)request.getAttribute( "adOtherBuyList" );
					List< AdSell > adSellList = (List< AdSell >)request.getAttribute( "adSellList" );
					List< AdSell > adOtherSellList = (List< AdSell >)request.getAttribute( "adOtherSellList" );
					
					List< List< String > > usersAllowedBuy = (List< List< String > >)request.getAttribute( "usersAllowedBuy" ); //$NON-NLS-1$
					List< List< String > > usersAllowedSell = (List< List< String > >)request.getAttribute( "usersAllowedSell" ); //$NON-NLS-1$
				%>
					<h1 align="center">Mes annonces</h1>
					<%
						boolean atLeastOne = false;
						if ( ( adBuyList != null )  && ( !adBuyList.isEmpty() ) )
						{
							atLeastOne = true;
					%>

					<h2 align="left">Mes annonces d'achat</h2>
					<div id="searchAd">
						<%= displayBuyList( adBuyList, true, usersAllowedBuy ) %>
					</div>
					<%
						}

						if ( ( adOtherBuyList != null )  && ( !adOtherBuyList.isEmpty() ) )
						{
							atLeastOne = true;
					%>
					<h2 align="left">Autres annonces d'achat me concernant</h2>
					<div id="searchAd">
						<%= displayBuyList( adOtherBuyList, false, null ) %>
					</div>
					<%
						}

						if ( ( adSellList != null )  && ( !adSellList.isEmpty() ) )
						{
							atLeastOne = true;
					%>
					<h2 align="left">Mes annonces de vente</h2>
					<div id="searchAd">
						<%= displaySellList( adSellList, true, usersAllowedSell ) %>
					</div>
					<%
						}

						if ( ( adOtherSellList != null )  && ( !adOtherSellList.isEmpty() ) )
						{
							atLeastOne = true;
					%>
					<h2 align="left">Autres annonces de vente me concernant</h2>
					<div id="searchAd">
						<%= displaySellList( adOtherSellList, false, null ) %>
					</div>
					<%
						}
						
						if ( !atLeastOne )
						{
					%>
							<div id="searchAd">
								<div id="resultEntry">
									<div id="msgEntryTitle">
										Vous n'avez publié ou répondu à aucune annonce
									</div>
								</div>
							</div>
					<%
						}
					%>
			</div>
            
            <jsp:directive.include file="footer.jsp"/>
        </div>
    </body>
</html>